//
// Created by madison on 2022/6/19.
//

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    /**
     * 方法一：动态规划
     * @param prices
     * @return
     */
    int maxProfit(vector<int> &prices) {
        int n = prices.size();
        int dp[n][2];
        dp[0][0] = 0, dp[0][1] = -prices[0];
        for (int i = 1; i < n; ++i) {
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[n - 1][0];
    }

    int maxProfit1(vector<int> &prices) {
        int n = prices.size();
        int dp0 = 0, dp1 = -prices[0];
        for (int i = 1; i < n; ++i) {
            int newDp0 = max(dp0, dp1 + prices[i]);
            int newDp1 = max(dp1, dp0 - prices[i]);
            dp0 = newDp0;
            dp1 = newDp1;
        }
        return dp0;
    }

    int maxProfit2(vector<int> &prices) {
        int ans = 0;
        int n = prices.size();
        for (int i = 1; i < n; ++i) {
            ans += max(0, prices[i] - prices[i - 1]);
        }
        return ans;
    }
};

int main() {
    vector<int> prices = {7, 1, 5, 3, 6, 4};
    Solution solution;
    cout << solution.maxProfit2(prices) << endl;
    return 0;
}